fix(resource): enforce agent-level watch task isolation#762
Merged
myysy merged 3 commits intovolcengine:mainfrom Mar 19, 2026
Merged
fix(resource): enforce agent-level watch task isolation#762myysy merged 3 commits intovolcengine:mainfrom
myysy merged 3 commits intovolcengine:mainfrom
Conversation
qin-ptr
approved these changes
Mar 19, 2026
Contributor
qin-ptr
left a comment
There was a problem hiding this comment.
Review Summary
经过详细分析,这个 PR 正确地修复了 watch task 权限检查中缺少 agent 级别隔离的安全问题。
核心变更
- 将 USER 角色的权限检查从
account_id + user_id增强为account_id + user_id + agent_id - 所有调用权限检查的方法都增加了
agent_id参数 - 在
ResourceService和WatchScheduler的调用链中正确传递了ctx.user.agent_id - ADMIN 和 ROOT 角色的行为保持不变
设计评估
✅ 问题真实性:确认问题存在,之前的代码允许同一用户下的不同 agent 访问彼此的 watch tasks
✅ 方案合理性:修改在正确的层级(_check_permission 是权限检查的核心方法),直接针对根因
✅ 实现完整性:所有调用路径都正确传递 agent_id,包括查询、更新、删除操作
✅ 测试覆盖:新增 156 行测试代码,覆盖跨 agent 访问、权限拒绝、ADMIN 管理、URI 冲突检测等关键场景
设计决策
PR 采用了 "agent 间隔离 + URI 全局冲突检测" 的设计:
- 不同 agent 不能看到彼此的 tasks(隔离性)
- 但仍然防止不同 agent 创建指向相同 URI 的 tasks(避免重复监控)
- 测试
test_conflict_when_task_exists_but_hidden_by_other_agent验证了这个行为是正确的
亮点
- 测试覆盖全面,包括单元测试和集成测试
- 错误消息更新为
{account_id}/{user_id}/{agent_id},提供更清晰的调试信息 - Commit 组织清晰:核心修复 + style 改进分离
- 考虑了参数默认值,减少潜在的破坏影响
建议 (non-blocking)
- 可以考虑添加测试验证 ROOT 角色可以跨 agent 访问(虽然代码逻辑简单,风险较低)
- 可以在 docstring 中更明确地说明设计决策
没有发现任何 blocking issues,代码正确,设计合理。
🤖 I am a bot owned by @qin-ctx.
myysy
approved these changes
Mar 19, 2026
Collaborator
myysy
left a comment
There was a problem hiding this comment.
Thank you for the comprehensive feature support.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes missing agent-level isolation for watch task permissions.
Previously, watch task access for
USERrole was effectively scoped byaccount_id + user_id, which allowed one agent to read, update, or delete watch tasks created by another agent under the same user.This PR tightens the permission check to
account_id + user_id + agent_idforUSERrole while preserving the existing behavior forADMINandROOT.Related Issue
N/A
Type of Change
Changes Made
WatchManagerpermission checks soUSERaccess now requires both matchinguser_idandagent_idctx.user.agent_idthroughResourceServiceand scheduler paths when querying, updating, and deleting watch tasksTesting
Test commands:
.venv/bin/python -m pytest tests/resource/test_watch_manager.py tests/service/test_resource_service_watch.py -qAdditional validation:
Checklist
Screenshots (if applicable)
N/A
Additional Notes
ADMINbehavior unchanged: admins can still manage watch tasks across agents within the same account.user + agentisolation model for agent-scoped state.